<?php

require_once(dirname(__FILE__) . '/../Query.php');

class Rass_MetaQuery extends RawsQuery
{
  public function __construct($kind = null, $paginate_by = null, $page = null)
  {
      parent::__construct();
      $this->setKind($kind);
      $this->setPaginateBy($paginate_by);
      $this->setPage($page);
  }

  public function setKind($value)
  {
    if ($value !== null) {
        $this->_params['kind'] = $value;
    } else {
        unset($this->_params['kind']);
    }
    return $this;
  }

  public function setPaginateBy($value)
  {
    if ($value !== null) {
        $this->_params['paginate_by'] = $value;
    } else {
        unset($this->_params['paginate_by']);
    }
    return $this;
  }
  
  public function setPage($value)
  {
    if ($value !== null) {
        $this->_params['page'] = $value;
    } else {
        unset($this->_params['page']);
    }
    return $this;
  }

  public function setName($value)
  {
    if ($value !== null) {
        $this->_params['name'] = $value;
    } else {
        unset($this->_params['name']);
    }
    return $this;
  }
  
  /**
   * Set the query-string argument and value(s) for doing a metadata search.
   *
   * Search arguments supported are: 'tag', 'qtag', 'iqtag', 'meta', 'qmeta' and 'iqmeta'.
   * Note that a single request (= Rass_MetaQuery object) may only contain a single metadata search argument.
   *
   * @param string $argname The search argument name (= tag', 'qtag', 'iqtag', 'meta', 'qmeta' or 'iqmeta').
   * @param array $or_args An array of argument values (= strings) for use by the OR operator.
   * @param array $and_args An array of argument values (= strings) for use by the AND operator.
   * @param array $not_args An array of argument values (= strings) for use by the NOT operator.
   * @return Rass_MetaFeed
   */
  public function setSearchArg($argname, $or_args, $and_args = null, $not_args = null)
  {
    $value = "";
    if ($argname !== null) 
    {
      if (($or_args) || ($and_args) || ($not_args))
      {
        # compose the string to be set as argument value
        if ($or_args) {
          $value = implode(",", $or_args);
        }
        if ($and_args) {
          foreach ($and_args as $a) {
            if ($value) {
              $value = rtrim($value, ',');
              $value .= ",";
            }
            $value .= "+" . $a;
          }
        }
        if ($not_args) {
          foreach ($not_args as $a) {
            if ($value) {
              $value = rtrim($value, ',');
              $value .= ",";
            }
            $value .= "-" . $a;
          }
        }
        # set the query-string argument
        if ($value) {
          $this->_params[$argname] = $value;
        }
      }
    } 
    return $this;
  }
  
  public function unsetSearchArg($argname)
  {
    unset($this->_params[$argname]);
  }
  
  public function getQueryUrl($base_uri = null, $resource = null, $path = null)
  {
    return parent::getQueryUrl($base_uri, "meta", $path);
  }

}
